package edu.cuc.chap02.lianxi.l2.Server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class TimeServerl2 {
    public static void main(String[] args) {
        new TimeServerl2().bind(9999);
    }

    protected void bind(int port) {
        EventLoopGroup workGroup = new NioEventLoopGroup();
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        try {

            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(workGroup, bossGroup).channel(NioServerSocketChannel.class).childOption(ChannelOption.SO_BACKLOG, 1024)
                    .childHandler(new ChannelInitializer<Channel>() {
                        @Override
                        protected void initChannel(Channel ch) throws Exception {
                            ch.pipeline().addLast(new TimeServerHandlerl2());
                        }
                    });
            ChannelFuture f = bootstrap.bind(port);
            System.out.println("=====绑定端口"+port+"=====");
            f.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            workGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }

    }
}
